using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.SystemModule;

namespace DomainComponents.XCRM
{
    public enum OrderStatus
    { 
        None = 0,
        [DevExpress.Xpo.DisplayName("No Money")]
        NoMoney = 1,
        [DevExpress.Xpo.DisplayName("Customer Canceled")]
        CustomerCanceled = 2
    }

    [DomainComponent]
    public interface IOrderCustomer : ICustomer {
        [BackReferenceProperty("BackRefCustomer")]
        IList<IOrder> Orders { get;  }
    }

    [DomainComponent]
    public interface IOrderOpportunity : IOpportunity {
        [BackReferenceProperty("BackRefOpportunity")]
        IList<IOrder> Orders { get;  }
    }

    [DomainComponent]
    public interface IOrderTarget : IInvoice
    {
        [VisibleInListView(false)]
        IOrder SourceOrder { get; set; }
    }

    [DomainComponent]
    [VisibleInReports]
    [ImageName("BO_Order")]
    [Appearance("Disable IOrder by StatusReason", TargetItems = "*", Criteria = "Status != 'None'", Enabled = false)]
    [ListViewFilter("Closed Orders", "[Status] != 'None'", "Closed Orders", Index = 0)]
    [ListViewFilter("My Open Orders", "[Owner.Oid] = custom('CurrentUserId') AND [Status] = 'None'", "My Open Orders", true, Index = 1)]
    [ListViewFilter("Open Orders", "[Status] == 'None'", "Open Orders", Index = 2)]
    [XafDisplayName("Order")]
    public interface IOrder : ISaleBase, IOpportunityTarget
    {
        OrderStatus Status { get; set; }

        [VisibleInListView(false)]
        DateTime CancelDate { get; set; }

        [VisibleInListView(false)]
        IQuote SourceQuote { get; set; }

        [System.ComponentModel.Browsable(false)]
        [VisibleInListView(false)]
        IOrderCustomer BackRefCustomer { get; set; }//TODO: duplicated with IInvoce, IOpportunity, IOrder, IQuote

        [System.ComponentModel.Browsable(false)]
        [VisibleInListView(false)]
        IOrderOpportunity BackRefOpportunity { get; set; }//TODO

        [BackReferenceProperty("SourceOrder")]
        IList<IOrderTarget> Invoices { get; }

        [Action(PredefinedCategory.View, Caption = "Cancel Order...", AutoCommit = true,
            TargetObjectsCriteria = "Status = 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Cancel(CancelOrderParameters parameters);

        [Action(PredefinedCategory.View, Caption = "Create Invoice...", AutoCommit = true,
          SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void CreateInvoiceFromOrder();

        [CreateInstance]
        IInvoice CreateInvoice();
    }


    [DomainLogic(typeof(IOrder))]
    public class OrderLogic
    {
        public static void CreateInvoiceFromOrder(IOrder order)
        {
            IOrderTarget invoice = (IOrderTarget)order.CreateInvoice();
            order.Copy(invoice);
            order.Invoices.Add(invoice);
        }
        public static void AfterChange_BackRefCustomer(IOrder saleBase)
        {
            saleBase.PotentialCustomer = saleBase.BackRefCustomer;
        }
        public static void AfterChange_PotentialCustomer(IOrder saleBase)
        {
            saleBase.BackRefCustomer = (IOrderCustomer)saleBase.PotentialCustomer;
        }
        public static void AfterChange_BackRefOpportunity(IOrder saleBase)
        {
            saleBase.SourceOpportunity = saleBase.BackRefOpportunity;
        }
        public static void AfterChange_SourceOpportunity(IOrder saleBase)
        {
            saleBase.BackRefOpportunity = (IOrderOpportunity)saleBase.SourceOpportunity;
        }
    }

    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class Order : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public Order(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
